package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant.*;

@Service
public class LevelServiceImpl implements LevelService {

    @Autowired
    LevelMapper levelMapper;

    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    @Autowired
    LevelConverter levelConverter;

    // 添加等级(后台等级权限管理-等级管理)
    @OperateLog(dataType = "等级管理", operateType = OPERATE_ADD)
    @Override
    public void addLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        levelMapper.insert(levelDO);
    }

    // 修改等级(后台等级权限管理-等级管理)
    @OperateLog(dataType = "等级管理", operateType = OPERATE_UPDATE)
    @Override
    public void updateLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        levelMapper.updateById(levelDO);
    }

    // 分页查询等级(后台等级权限管理-等级管理)
    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        // 构造查询条件
        LambdaQueryWrapper<LevelDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(request.getKeyword()!= null,LevelDO::getLevelName, request.getKeyword());

        // 构造分页对象
        Page<LevelDO> page = new Page<>(request.getPageNum(), request.getCount());

        // 执行查询
        Page<LevelDO> resultPage = levelMapper.selectPage(page, queryWrapper);

        List<LevelDO> records = resultPage.getRecords();
        long total = resultPage.getTotal();

        PageDTO<LevelDTO> levelDTOPageDTO = levelConverter.levelDOToPageDTO(records, (int)total);

        return levelDTOPageDTO;
    }

    // 删除等级(后台等级权限管理-等级管理)
    @OperateLog(dataType = "等级管理", operateType = OPERATE_SINGLE_DELETE)
    @Override
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        LevelDO levelDO = levelMapper.selectById(id);
        DeleteLevelOperateDTO deleteLevelOperateDTO = levelConverter.levelDOToDeleteLevelOperateDTO(levelDO);

        // Todo: 删除 表level_privilege 中的记录
        LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LevelPrivilegeDO::getLevelId, id);
        levelPrivilegeMapper.delete(queryWrapper);

        // Todo: 删除 表level 中的记录
        levelMapper.deleteById(id);

        return deleteLevelOperateDTO;
    }

    // 批量删除等级(后台等级权限管理-等级管理)
    @OperateLog(dataType = "等级管理", operateType = OPERATE_BATCH_DELETE)
    @Override
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        // 获取要删除的id列表
        List<String> ids = command.getIds();
        List<LevelDO> levelDOS = levelMapper.selectByIds(ids);
        List<DeleteLevelOperateDTO> deleteLevelOperateDTOS = levelConverter.levelDOsToDeleteLevelOperateDTOs(levelDOS);

        // Todo: 删除 表level_privilege 中的记录
        LambdaQueryWrapper<LevelPrivilegeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(LevelPrivilegeDO::getLevelId, ids);
        levelPrivilegeMapper.delete(queryWrapper);

        // Todo: 删除 表level 中的记录
        levelMapper.deleteByIds(ids);

        return deleteLevelOperateDTOS;
    }
}
